package common;

import java.util.HashMap;

import junit.framework.Assert;
import junit.framework.TestCase;

public class WordHistogramTests extends TestCase {

  protected void setUp() throws Exception {

  }

  /*
   public WordHistogram(char[] breakChars, StopWordFile stopWordFile, File fileToCount) throws FileNotFoundException, IOException {
    wordMap = new HashMap();
    BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(fileToCount));
    IndexWordUtil wordUtil = new IndexWordUtil(breakChars);
    for (String word = wordUtil.getNextWord(inputStream); word != null; word = wordUtil.getNextWord(inputStream)) {
      totalWordCount++;
      word = word.toLowerCase();
      if (!stopWordFile.isStopWord(word)) {
        incrementWordCount(word);
      }
    }
  }
  */

  public void testWordHistogram() {

  }

  /**
   * Trims off all words with a count equal to the threshold, then applies the upper
   * and lower bound and returns a trimmed map with the max results specified.<br>
   * Expects a Map:<br>
   * key = word (String)<br>
   * value = count (Integer)
   * 
   * @param upperBound a percentage of the upper bound to trim off
   * @param lowerBound a percentage of the lower bound to trim off
   * @param maxResults the max number of results desired in the map, 
   *                   set to null if all possible results (after the trimming) are desired
   * @return the trimmed Map
   */
  /*
   public static Map trim(Map mapToTrim, int threshold, double upperBound, double lowerBound, Integer maxResults) {
   //  add all the words to a list
   ArrayList listOfWords = new ArrayList();
   Iterator itr = mapToTrim.keySet().iterator();
   while (itr.hasNext()) {
   Object word = itr.next();
   Integer count = (Integer) mapToTrim.get(word);
   if(count.intValue() > threshold) 
   listOfWords.add(new HistoWord(word.toString(), count));
   else
   itr.remove();
   }
   
   //order the list then do the trimming
   Collections.sort(listOfWords); //sorted in ascending order  
   int listSize = listOfWords.size();
   
   double trimUpper = Math.floor(upperBound * listSize); //number of words to trim from the upper portion of the list
   if(trimUpper < listSize) {
   int index = listSize;
   for(int i = 0; i < trimUpper; i++) {
   index--;
   HistoWord word = (HistoWord)listOfWords.remove(index);
   mapToTrim.remove(word.text);
   }
   }
   
   double trimLower = Math.floor(lowerBound * listSize); //number of words to trim from the lower portion of the list
   if(trimLower < listSize) {
   for(int i = 0; i < trimLower; i++) {
   HistoWord word = (HistoWord)listOfWords.remove(i);
   mapToTrim.remove(word.text);
   }
   }
   
   if(maxResults != null && listOfWords.size() > 0) {
   while(mapToTrim.size() > maxResults.intValue()) {
   HistoWord word = (HistoWord)listOfWords.remove(0);
   mapToTrim.remove(word.text);
   }          
   }
   
   return mapToTrim;
   }
   */
  public void testTrim() {
    HashMap testmap1 = new HashMap();
    testmap1.put("first", new Integer(1));
    testmap1.put("second", new Integer(2));
    testmap1.put("third", new Integer(3));
    testmap1.put("fourth", new Integer(4));
    testmap1.put("fifth", new Integer(5));
    testmap1.put("sixth", new Integer(6));
    testmap1.put("seventh", new Integer(7));
    testmap1.put("eighth", new Integer(8));
    testmap1.put("ninth", new Integer(9));

    HashMap resultmap1 = new HashMap();
    resultmap1.put("third", new Integer(3));
    resultmap1.put("fourth", new Integer(4));
    resultmap1.put("fifth", new Integer(5));
    resultmap1.put("sixth", new Integer(6));
    resultmap1.put("seventh", new Integer(7));

    HashMap resultmap2 = new HashMap();
    resultmap2.put("sixth", new Integer(6));
    resultmap2.put("seventh", new Integer(7));

    HashMap resultmap3 = new HashMap();
    resultmap3.put("seventh", new Integer(7));
    resultmap3.put("eighth", new Integer(8));

    HashMap resultmap4 = new HashMap();
    resultmap4.put("sixth", new Integer(6));
    resultmap4.put("seventh", new Integer(7));
    resultmap4.put("eighth", new Integer(8));

    HashMap resultmap5 = new HashMap();
    resultmap5.put("ninth", new Integer(9));

    Assert.assertEquals(WordHistogram.trim(testmap1, 1, .8, .2, new Integer(8)), resultmap1);
    Assert.assertEquals(WordHistogram.trim(testmap1, 4, .8, .2, new Integer(8)), resultmap2);
    Assert.assertEquals(WordHistogram.trim(testmap1, 4, 8, .2, new Integer(2)), resultmap3);
    Assert.assertEquals(WordHistogram.trim(testmap1, 4, .8, 0, new Integer(3)), resultmap4);
    Assert.assertEquals(WordHistogram.trim(testmap1, 1, 1, .2, new Integer(1)), resultmap5);

    //Test for negative values for max results, threshold, and lower bound, as well as 
    //values > 1 for upperbound. How do I do this?  Maybe with assertTrue/False()?

    //		Assert.assertEquals(WordHistogram.trim(testmap1, 1, .8, .2, new Integer(-2)), resultmap5);
    //		Assert.assertEquals(WordHistogram.trim(testmap1, -2, .8, .2, new Integer(1)), resultmap5);
    //		Assert.assertEquals(WordHistogram.trim(testmap1, 1, .8, -.1, new Integer(1)), resultmap5);
    //		Assert.assertEquals(WordHistogram.trim(testmap1, 1, 1.1, .2, new Integer(1)), resultmap5);

  }

  public static void main(String[] args) {
    junit.textui.TestRunner.run(WordHistogramTests.class);
  }

}
